//
//  LinegraphView.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/9/5.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "LinegraphView.h"
@interface LinegraphView(){
    CGFloat _rowSpace;
    CGFloat _colSpace;
    
    CGFloat _rowMinY;
    CGFloat _rowMaxY;
    
    CGFloat _colMinX;
    CGFloat _colMaxX;
    
}

@property(nonatomic,strong)NSArray * rowDatas;

@property(nonatomic,strong)NSArray * colDatas;

@property(nonatomic,strong)NSMutableArray * points;

@property(nonatomic,strong)UIBezierPath * path;

@property(nonatomic,strong)LinegraphPopView * popView;

@property(nonatomic,strong)UIButton * selectedButton;
@end
@implementation LinegraphView
-(id)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.backgroundColor = HexColor(@"#EEEEEE");
        self.points = [NSMutableArray array];
        
        self.maxRowValue = 1;
        self.minRowValue = 0;
        self.rows = 7;
        
        self.maxColValue = 1;
        self.minColValue = 0;
        self.cols = 7;
        
        [self setUpTable];
        
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handelNoti:) name:@"DatePickerPopViewSelectedDate" object:nil];
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenPopView)]];
        
    }
    return self;
}

#pragma mark - 处理通知
-(void)handelNoti:(NSNotification*)noti{
 
}
-(void)hiddenPopView{
    [self.popView removeFromSuperview];
}
-(void)setUpTable{
    for (UIView * v in self.subviews) {
        [v removeFromSuperview];
    }
    
    _colSpace = (self.width-Bill_width(30)-2*globalMargin)/(self.cols-1);
    _rowSpace = (self.height-Bill_height(10)-2*globalMargin-30)/(self.rows-1);
    UILabel * lastLabel = nil;
    UILabel * firstLabel = nil;
    
    CGFloat stepRowValue = (self.maxRowValue-self.minRowValue)/(self.rows-1);
    for (int i = 0; i<self.rows; i++) {
        NSString * str = [NSString stringWithFormat:@"%.0f",(self.maxRowValue-i*stepRowValue)];
        UILabel * l = [self labelWith:str];
        l.x = Bill_width(30)-l.width;
        l.x = Bill_width(30)-l.width;
        l.y = (i)*(_rowSpace)+globalMargin;
        [self addSubview:l];
        
        UIView * v = [[UIView alloc] initWithFrame:CGRectZero];
        v.backgroundColor = [UIColor blackColor];
        v.height = 0.5;
        v.width = self.width - l.right - 2*globalMargin;
        v.x = l.right+3;
        v.centerY = l.centerY;
        [self addSubview:v];
        
        if(i==_rows-1){
            firstLabel = l;
            _rowMaxY = l.centerY;
        }
        if(i==0){
            lastLabel = l;
            _rowMinY = l.centerY;
        }
        
    }
    
    CGFloat stepColValue = (self.maxColValue - self.minColValue)/(self.cols-1);
    for (int i =0 ; i<self.cols; i++) {
        NSString * str = [NSString stringWithFormat:@"%.0f",(self.minColValue+i*stepColValue)];
        UILabel * l = [self labelWith:str];
        l.centerX = firstLabel.right+i*(_colSpace)+3;
        l.y = firstLabel.botton+globalMargin-7;
        [self addSubview:l];
        
        UIView * v = [[UIView alloc] initWithFrame:CGRectZero];
        v.backgroundColor = [UIColor blackColor];
        v.width = 0.5;
        v.height= fabs(firstLabel.centerY - lastLabel.centerY);
        v.centerX = l.centerX;
        v.y = lastLabel.centerY;
        [self addSubview:v];
        if(i == 0){
            _colMinX = l.centerX;
        }
        if(i == self.cols-1){
            _colMaxX = l.centerX;
        }
    }
}


-(void)setRowDatas:(NSArray *)rowDatas colDatas:(NSArray *)colDatas{
    self.rowDatas = rowDatas;
    self.colDatas = colDatas;
    
    if(rowDatas.count == colDatas.count){
        [self.points removeAllObjects];
        for (int i =0 ; i<rowDatas.count; i++) {
            CGFloat y = (1-([rowDatas[i] floatValue]/self.maxRowValue))*(_rowMaxY-_rowMinY)+_rowMinY;
            
            CGFloat x = ([colDatas[i] floatValue]/self.maxColValue)*(_colMaxX-_colMinX)+_colMinX;
            
            CGPoint p = CGPointMake(x, y);
            
            [self.points addObject:[NSValue valueWithCGPoint:p]];
        }
        int i = 0;
        for (NSValue * value in self.points) {
            CGPoint p = [value CGPointValue];
            UIButton * btn = [[UIButton alloc] initWithFrame:CGRectZero];
            [btn setImage:[UIImage imageNamed:@"Ellipse-1-copy-8"] forState:UIControlStateNormal];
            
            [btn setImage:[UIImage imageNamed:@"Ellipse-1-copy-9"] forState:UIControlStateSelected];
            btn.size = CGSizeMake(Bill_width(12), Bill_width(12));
            btn.center = p;
            btn.tag = i++;
            [btn addTarget:self action:@selector(pointButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:btn];
        }
        [self setNeedsDisplay];
    }
}
-(void)pointButtonClick:(UIButton*)button{
    [self.popView removeFromSuperview];
    
    CGFloat rowData =  [self.rowDatas[button.tag] floatValue];
    CGFloat colData =  [self.colDatas[button.tag] floatValue];
    NSString * date = [NSString stringWithFormat:@"%.0f月%.0f日",8.f,colData];
    if(self.selectedButton == button)
    {
        button.selected = !button.selected;
        if(button.selected){
            [self addSubview:self.popView];
            [self.popView setDate:date amount:rowData];
            self.popView.centerX = button.centerX;
            self.popView.y = button.botton+globalMargin;
            return;
        }
        else{
            return;
        }
        
    }
    button.selected = YES;
    if(self.selectedButton){
        self.selectedButton.size = [UIImage imageNamed:@"Ellipse-1-copy-9"].size;
        self.selectedButton.center = [self.points[self.selectedButton.tag] CGPointValue];
        self.selectedButton.selected = NO;
    }
    
    self.selectedButton = button;
    button.size = [UIImage imageNamed:@"Ellipse-1-copy-9"].size;
    button.center = [self.points[button.tag] CGPointValue];
        
    [self addSubview:self.popView];
    [self.popView setDate:date amount:rowData];
    self.popView.centerX = button.centerX;
    self.popView.y = button.botton+globalMargin;


}
-(void)drawRect:(CGRect)rect{
    
    [[UIColor blackColor] set];
    if(self.points.count!=0){
        [self.path removeAllPoints];
        
        for (int i = 0; i<self.points.count; i++) {
            CGPoint p = [self.points[i] CGPointValue];
            if(i == 0){
                [self.path moveToPoint:p];
            }
            else{
                [self.path addLineToPoint:p];
            }
        }
        [self.path stroke];
    }
    [super drawRect:rect];
}

-(UIBezierPath*)path{
    if(_path == nil){
        [HexColor(@"#3CB034") set];
        _path = [UIBezierPath bezierPath];
        _path.lineWidth = 2.5;
        _path.lineJoinStyle = kCGLineJoinRound;
    }
    return _path;
}

-(LinegraphPopView*)popView{
    if(_popView == nil){
        _popView = [[LinegraphPopView alloc] initWithFrame:CGRectMake(0, 0, 100, 25)];
        [_popView addRadius:3];
    }
    return _popView;
}

-(UILabel*)labelWith:(NSString*)text{
    UILabel * label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:12];
    label.text = text;
    label.size = [text sizeInAre:MaxSize font:label.font];
//    label.backgroundColor = RandColor;
    return label;
}

- (void)dealloc
{

    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"DatePickerPopViewSelectedDate" object:nil];
}
@end



@implementation LinegraphPopView

-(id)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.backgroundColor = [UIColor orangeColor];
        self.dateLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.dateLabel.font = [UIFont systemFontOfSize:11];
        self.dateLabel.textColor = [UIColor whiteColor];
        [self addSubview:self.dateLabel];
        
        self.amountLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.amountLabel.font = [UIFont systemFontOfSize:11];
        self.amountLabel.textColor = [UIColor whiteColor];
        [self addSubview:self.amountLabel];
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    self.dateLabel.size = [self.dateLabel.text sizeInAre:MaxSize font:self.dateLabel.font];
    self.dateLabel.x = globalMargin;
    self.dateLabel.centerY = self.height * 0.5;
    
    self.amountLabel.size = [self.amountLabel.text sizeInAre:MaxSize font:self.amountLabel.font];
    self.amountLabel.x = self.width - self.amountLabel.width - globalMargin;
    self.amountLabel.centerY = self.height * 0.5;
    
}

-(void)setDate:(NSString*)date amount:(CGFloat)amount{
    self.dateLabel.text = date;
    self.amountLabel.text = [NSString stringWithFormat:@"%.0f元",amount];
    [self setNeedsLayout];
}
@end